/*
 * Copyright (c) 2018-2999 北京小象智慧信息科技有限公司 All rights reserved.
 *
 * https://www.xiaoxiangai.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.app.dto.MyOrderItemDto;
import com.yami.shop.bean.app.dto.ProductDto;
import com.yami.shop.bean.pt.YxPtgl;
import com.yami.shop.common.util.PageAdapter;
import com.yami.shop.dao.YxPtglMapper;
import jodd.util.CollectionUtil;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yami.shop.bean.app.dto.MyOrderDto;
import com.yami.shop.bean.model.Order;
import com.yami.shop.dao.OrderMapper;
import com.yami.shop.service.MyOrderService;

/**
 * @author lgh on 2018/09/15.
 */
@Service
public class MyOrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements MyOrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private YxPtglMapper yxPtglMapper;

    private static final Logger log = LoggerFactory.getLogger(MyOrderServiceImpl.class);


    @Override
    public IPage<MyOrderDto> pageMyOrderByUserIdAndStatus(Page<MyOrderDto> page, String userId, Integer status) {
        List<MyOrderDto> myOrderDtoList = orderMapper.listMyOrderByUserIdAndStatus(new PageAdapter(page), userId, status);
        if(CollectionUtils.isNotEmpty(myOrderDtoList)){
            for(MyOrderDto myOrderDto:myOrderDtoList){
                Long ptglid = myOrderDto.getPtglid();
                YxPtgl yxPtgl = yxPtglMapper.selectYxPtglById(ptglid);
                if(yxPtgl!=null){
                    myOrderDto.setPtstatus(yxPtgl.getStatus());
                }
            }
        }
        page.setRecords(myOrderDtoList);
      //  page.setTotal(orderMapper.countMyOrderByUserIdAndStatus(userId, status));
        return page;
    }

    @Override
    public IPage<MyOrderDto> pageMyOrderByUserIdAndRefundState(Page<MyOrderDto> page, String userId) {
        page.setRecords(orderMapper.listMyOrderByUserIdAndRefundState(new PageAdapter(page), userId));
        page.setTotal(orderMapper.countMyOrderByUserIdAndRefundState(userId));
        return page;
    }

}
